package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldRegularPlan;
import com.cleaningcloud.cld.domain.CldRegularPlanDetails;
import com.cleaningcloud.cld.domain.CldRegularSubmit;
import com.cleaningcloud.cld.domain.dto.CldRegularPlanDto;
import com.cleaningcloud.cld.domain.vo.CldRegularPlanDetailsVo;
import com.cleaningcloud.cld.domain.vo.CldRegularPlanVo;
import com.cleaningcloud.cld.mapper.CldRegularPlanDetailsMapper;
import com.cleaningcloud.cld.mapper.CldRegularPlanMapper;
import com.cleaningcloud.cld.mapper.CldRegularSubmitMapper;
import com.cleaningcloud.cld.service.ICldRegularPlanService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 例会计划Service业务层处理
 *
 * @author cleaningcloud
 * @date 2025-01-13
 */
@Service
public class CldRegularPlanServiceImpl implements ICldRegularPlanService {
    @Autowired
    private CldRegularPlanMapper cldRegularPlanMapper;

    @Autowired
    private CldRegularPlanDetailsMapper cldRegularPlanDetailsMapper;

    @Autowired
    private CldRegularSubmitMapper cldRegularSubmitMapper;


    /**
     * 查询例会计划
     *
     * @param regularPlanId 例会计划主键
     * @return 例会计划
     */
    @Override
    public CldRegularPlanVo selectCldRegularPlanByRegularPlanId(Long regularPlanId) {
        CldRegularPlanVo cldRegularPlanVo = cldRegularPlanMapper.selectCldRegularPlanByRegularPlanId(regularPlanId);
        CldRegularPlanDetails query = new CldRegularPlanDetailsVo();
        query.setRegularPlanId(cldRegularPlanVo.getRegularPlanId());
        cldRegularPlanVo.setDetailsList(cldRegularPlanDetailsMapper.selectCldRegularPlanDetailsList(query));
        return cldRegularPlanVo;
    }

    /**
     * 查询例会计划列表
     *
     * @param cldRegularPlan 例会计划
     * @return 例会计划
     */
    @Override
    public List<CldRegularPlanVo> selectCldRegularPlanList(CldRegularPlan cldRegularPlan) {
        return cldRegularPlanMapper.selectCldRegularPlanList(cldRegularPlan);
    }

    /**
     * 新增例会计划
     *
     * @param cldRegularPlan 例会计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCldRegularPlan(CldRegularPlanDto cldRegularPlanDto) {
        cldRegularPlanDto.setCreateTime(DateUtils.getNowDate());
        cldRegularPlanDto.setFlag(0L);
        cldRegularPlanDto.setCreateBy(SecurityUtils.getUsername());
        if (cldRegularPlanDto.getDetailsList().isEmpty()) {
            throw new RuntimeException("请添加例会明细！");
        }
        cldRegularPlanMapper.insertCldRegularPlan(cldRegularPlanDto);
        for (CldRegularPlanDetails cldRegularPlanDetails : cldRegularPlanDto.getDetailsList()) {
            cldRegularPlanDetails.setTitle(cldRegularPlanDetails.getTitle());
            cldRegularPlanDetails.setRegularPlanId(cldRegularPlanDto.getRegularPlanId());
        }
        cldRegularPlanMapper.batchInsertRegularDetails(cldRegularPlanDto.getDetailsList());
        // 新增例会提交明细
        List<CldRegularPlanDetails> detailsList = cldRegularPlanDetailsMapper.selectCldRegularPlanDetailsByPlanId(cldRegularPlanDto.getRegularPlanId());
        List<CldRegularSubmit> submitList = new ArrayList<>();
        for (String staffId : cldRegularPlanDto.getStaffId().split(",")) {
            for (CldRegularPlanDetails cldRegularPlanDetails : detailsList) {
                CldRegularSubmit cldRegularSubmit = new CldRegularSubmit();
                cldRegularSubmit.setRegularPlanId(cldRegularPlanDetails.getRegularPlanId());
                cldRegularSubmit.setStaffId(Long.valueOf(staffId));
                cldRegularSubmit.setPlanDetailsId(cldRegularPlanDetails.getPlanDetailsId());
                cldRegularSubmit.setSubmitStatus(0L);
                submitList.add(cldRegularSubmit);
            }
        }

        return cldRegularPlanMapper.batchInsertRegularSubmit(submitList);
    }

    /**
     * 修改例会计划
     *
     * @param cldRegularPlan 例会计划
     * @return 结果
     */
    @Override
    public int updateCldRegularPlan(CldRegularPlanDto cldRegularPlanDto) {
        cldRegularPlanMapper.deleteCldRegularPlanDetails(cldRegularPlanDto.getRegularPlanId());
        for (CldRegularPlanDetails cldRegularPlanDetails : cldRegularPlanDto.getDetailsList()) {
            cldRegularPlanDetails.setTitle(cldRegularPlanDetails.getTitle());
            cldRegularPlanDetails.setRegularPlanId(cldRegularPlanDto.getRegularPlanId());
        }
        cldRegularPlanMapper.batchInsertRegularDetails(cldRegularPlanDto.getDetailsList());
        // 新增例会提交明细
        cldRegularPlanMapper.deleteCldRegularPlanSubmit(cldRegularPlanDto.getRegularPlanId());
        List<CldRegularPlanDetails> detailsList = cldRegularPlanDetailsMapper.selectCldRegularPlanDetailsByPlanId(cldRegularPlanDto.getRegularPlanId());
        List<CldRegularSubmit> submitList = new ArrayList<>();
        for (String staffId : cldRegularPlanDto.getStaffId().split(",")) {
            for (CldRegularPlanDetails cldRegularPlanDetails : detailsList) {
                CldRegularSubmit cldRegularSubmit = new CldRegularSubmit();
                cldRegularSubmit.setRegularPlanId(cldRegularPlanDetails.getRegularPlanId());
                cldRegularSubmit.setStaffId(Long.valueOf(staffId));
                cldRegularSubmit.setPlanDetailsId(cldRegularPlanDetails.getPlanDetailsId());
                cldRegularSubmit.setSubmitStatus(0L);
                submitList.add(cldRegularSubmit);
            }
        }
        return cldRegularPlanMapper.updateCldRegularPlan(cldRegularPlanDto);
    }

    /**
     * 批量删除例会计划
     *
     * @param regularPlanIds 需要删除的例会计划主键
     * @return 结果
     */
    @Override
    public int deleteCldRegularPlanByRegularPlanIds(Long[] regularPlanIds) {
        return cldRegularPlanMapper.deleteCldRegularPlanByRegularPlanIds(regularPlanIds);
    }

    /**
     * 删除例会计划信息
     *
     * @param regularPlanId 例会计划主键
     * @return 结果
     */
    @Override
    public int deleteCldRegularPlanByRegularPlanId(Long regularPlanId) {
        return cldRegularPlanMapper.deleteCldRegularPlanByRegularPlanId(regularPlanId);
    }

    @Override
    public int submitCldRegularSubmitPlan(CldRegularPlanDto cldRegularPlan) {
        if (cldRegularPlan.getSubmitIds().isEmpty()) {
            throw new RuntimeException("请选择例会计划明细！");
        }
        for (Long submitId : cldRegularPlan.getSubmitIds()) {
            CldRegularSubmit cldRegularSubmit = new CldRegularSubmit();
            cldRegularSubmit.setRegularSubmitId(submitId);
            cldRegularSubmit.setSubmitTime(DateUtils.getNowDate());
            cldRegularSubmit.setSubmitStatus(1L);
            cldRegularSubmitMapper.updateCldRegularSubmit(cldRegularSubmit);
        }
        return 1;
    }
}
